package com.yixin.hospital.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yixin.hospital.common.ApiResult;
import com.yixin.hospital.common.Constant;
import com.yixin.hospital.entity.domain.JobDutyMenu;
import com.yixin.hospital.entity.domain.Menu;
import com.yixin.hospital.entity.domain.UserMenu;
import com.yixin.hospital.service.JobDutyMenuService;
import com.yixin.hospital.service.MenuService;
import com.yixin.hospital.service.UserMenuService;
import com.yixin.hospital.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/menu")
@Api(value = "菜单管理", tags = "菜单管理")
public class MenuController {

    @Resource
    MenuService menuService;
    @Resource
    UserMenuService userMenuService;
    @Resource
    JobDutyMenuService jobDutyMenuService;

    @GetMapping("/list")
    @ApiOperation(value = "菜单列表")
    public ApiResult list() {
        List<Menu> list = menuService.list();
        return ApiResult.success(list);
    }

    @GetMapping("/tree-list")
    @ApiOperation(value = "菜单列表（树）")
    public ApiResult listTree() {
        List<Menu> list = menuService.list();
        TreeNodeConfig nodeConfig = new TreeNodeConfig();
        List<Tree<Integer>> treeList = TreeUtil.build(list, 0, nodeConfig,
                (node, tree) -> {
                    tree.setId(node.getId().intValue());
                    tree.setParentId(node.getPid().intValue());
                    tree.setName(node.getName());
                    tree.putExtra("label", node.getName());
                });
        return ApiResult.success(CollectionUtil.isEmpty(treeList) ? new ArrayList<>() : treeList);
    }

    @GetMapping("/user-menus")
    @ApiOperation(value = "菜单列表 by token")
    public ApiResult listTree(HttpServletRequest request) {
        Long userId = JwtUtil.getUserId(request.getHeader(Constant.AUTHORIZATION));
        // 查询用户所有菜单权限
        LambdaQueryWrapper<UserMenu> umQueryWrapper = new LambdaQueryWrapper<>();
        umQueryWrapper.eq(UserMenu::getUserId, userId);
        List<UserMenu> list = userMenuService.list(umQueryWrapper);
        return ApiResult.success(list.stream().map(userMenu -> userMenu.getMenuId()).collect(Collectors.toList()));
    }

    @GetMapping("/user-menus/{userId}")
    @ApiOperation(value = "用户拥有的菜单列表")
    public ApiResult userMenus(@PathVariable int userId) {
        // 查询用户所有菜单权限
        LambdaQueryWrapper<UserMenu> umQueryWrapper = new LambdaQueryWrapper<>();
        umQueryWrapper.eq(UserMenu::getUserId, userId);
        List<UserMenu> list = userMenuService.list(umQueryWrapper);
        return ApiResult.success(list.stream().map(userMenu -> userMenu.getMenuId()).collect(Collectors.toList()));
    }

    @GetMapping("/user-menus-by-jobDuty/{jobDutyId}")
    @ApiOperation(value = "职务拥有得菜单列表")
    public ApiResult jobDutyMenus(@PathVariable int jobDutyId) {
        // 查询用户所有菜单权限
        LambdaQueryWrapper<JobDutyMenu> umQueryWrapper = new LambdaQueryWrapper<>();
        umQueryWrapper.eq(JobDutyMenu::getJobDutyId, jobDutyId);
        List<JobDutyMenu> list = jobDutyMenuService.list(umQueryWrapper);
        return ApiResult.success(list.stream().map(jobDutyMenu -> jobDutyMenu.getMenuId()).collect(Collectors.toList()));
    }

}
